#include"SetList.h"
void SeqListInit(SeqList* ps)
{
	ps->a = (SLDateType*)malloc(sizeof(SLDateType) * 4);
	if (ps->a == NULL)
	{
		perror("malloc");
		exit(-1);
	}
	ps->size = 0;
	ps->capacity = 4;
}
void SeqListExpansion(SeqList* ps)
{
	if (ps->size == ps->capacity)
	{
		SLDateType*tmp = (SLDateType*)realloc(ps->a, (sizeof(SLDateType) * ps->capacity) * 2);
		if (tmp == NULL)
		{
			perror("realloc failed");
			exit(-1);
		}
		ps->a = tmp;
		ps->capacity *= 2;
	}
}
void SeqListDestroy(SeqList* ps)
{
	free(ps->a);
	ps->a = NULL;
	ps->size = 0;
	ps->capacity = 0;
}
void SeqListPrint(SeqList* ps)
{
	int i = 0;
	for (i; i < ps->size; i++)
	{
		printf("%d ", *((ps->a) + i));
	}
	printf("\n");
}
void SeqListPushBack(SeqList* ps, SLDateType x)
{
	SeqListExpansion(ps);
	int i = 0;
	scanf("%d", &i);
	*((ps->a) + ps->size) = i;
	ps->size++;
}
void SeqListPopBack(SeqList* ps)
{
	assert(ps->size>0);
	if (ps->size > 0)
	{
		*(ps->a + (ps->size - 1)) = 0;
		ps->size--;
	}
}
void SeqListPushFront(SeqList* ps, SLDateType x)
{

	SeqListExpansion(ps);
	int n = 0;
	n = ps->size-1;
	while (n>=0)
	{
		*((ps->a) + n+1) = *((ps->a) +n );
		n--;
	}
	ps->size++;
	n += 1;
	*((ps->a) + n) = x;
}
void SeqListPopFront(SeqList* ps)
{
	assert(ps);
	int n = 0;
	for (n = 0; n < ps->size-1; n++)
	{
		*((ps->a) + n) = *((ps->a) + n + 1);
	}
	ps->size--;
}
//void SeqListPopFront(SeqList* ps)
//{
//	ps->a=memmove(ps, ps + 1, sizeof(SLDateType) * (ps->size - 1));
//	ps->size--;
//}
void SeqListInsert(SeqList* ps, int pos, SLDateType x)
{
	asssert(pos >= 0 && pos < ps->size);
	SeqListExpansion(ps);
	int n = 0;
	n = ps->size - 1;
	while (n >= pos)
	{
		*((ps->a) + n + 1) = *((ps->a) + n);
		n--;
	}
	*(ps->a + pos) = x;
	ps->size++;
}
void SeqListErase(SeqList* ps, int pos)
{
	assert(ps);
	int n = pos;
	for (n = pos; n < ps->size - 1; n++)
	{
		*((ps->a) + n) = *((ps->a) + n + 1);
	}
	ps->size--;
}